package com.qfedu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qfedu.entity.GoodsEntity;
import com.qfedu.entity.OrderEntity;
import com.qfedu.entity.RouteEntity;
import com.qfedu.entity.UserEntity;
import com.qfedu.service.GoodsService;
import com.qfedu.service.OrderService;
import com.qfedu.service.RouteService;
import com.qfedu.util.Ajaxd;
import com.qfedu.util.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class OrderController {
    @Reference(check = false)
    private OrderService orderService;

    @Reference(check = false)
    private RouteService routeService;

    @Reference(check = false)
    private GoodsService goodsService;
    @RequestMapping("/orderlist.to")
    public String toorderlist(){
        return "orderManager";
    }

    //订单列表
    @RequestMapping("/orderlist.do")
    @ResponseBody
    public Ajaxd doorderlist(int pageNum,@RequestParam(value = "pagesize",required = false,defaultValue = "4") int pageSize,String qutj, HttpServletRequest request){
        Ajaxd ajaxd = new Ajaxd();
        Page page = orderService.queryByPage(pageNum, pageSize,0,qutj,2);
        List<OrderEntity> orderlist = page.getOrderlist();
        for (OrderEntity orderEntity:orderlist){
            RouteEntity routeEntity = routeService.queryRouteById(orderEntity.getRid());
            String dataformat = dataformat(routeEntity.getTime());
            routeEntity.setTimes(dataformat);
            orderEntity.setRouteEntity(routeEntity);
        }
        page.setOrderlist(orderlist);
        if (page!=null){
            ajaxd.setPage(page);
            ajaxd.setFlag(true);
        }else {
            ajaxd.setFlag(false);
        }
        return ajaxd;
    }

    //订单详情
    @RequestMapping("/orderdetail.do")
    @ResponseBody
    public Ajaxd toorderdetail(String oid,HttpServletRequest request){
        Ajaxd ajaxd = new Ajaxd();
        OrderEntity queryone = orderService.queryone(oid);
        List<GoodsEntity> goodsEntities = goodsService.queryGoodsBYOid(oid);
        HttpSession session = request.getSession();
        session.setAttribute("goodlist",goodsEntities);
        session.setAttribute("oneorder",queryone);
        if (goodsEntities!=null){
            ajaxd.setFlag(true);
        }else{
            ajaxd.setFlag(false);
        }
        return ajaxd;
    }

    @RequestMapping("/orderdetail.to")
    public String toorderdetal(){
        return "orderdetail";
    }

    //用户订单通知信息
    @RequestMapping("/updateDesc.do")
    @ResponseBody
    public Ajaxd douodatedesc(OrderEntity orderEntity){
        Ajaxd ajaxd = new Ajaxd();
        Boolean aBoolean = orderService.updateOrder(orderEntity);
        if(aBoolean){
            ajaxd.setFlag(true);
        }else{
            ajaxd.setFlag(false);
        }
        return ajaxd;
    }

    //删除订单
    @RequestMapping("/delByOid.do")
    @ResponseBody
    public Ajaxd doDelByOid(String oid){
        Boolean aBoolean = orderService.delByOid(oid);
        Ajaxd ajaxd = new Ajaxd();
        if (aBoolean){
            ajaxd.setFlag(true);
        }else {
            ajaxd.setFlag(false);
        }
        return ajaxd;
    }

    private String dataformat(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String format = simpleDateFormat.format(date);
        return format;
    }
}
